package vn.bk.hotel;

import java.util.ArrayList;
import java.util.List;


import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabContentFactory;

import com.actionbarsherlock.app.SherlockFragment;

public class Search extends SherlockFragment implements OnTabChangeListener,
	OnPageChangeListener {

    MyPageAdapter pageAdapter;
    private ViewPager mViewPager;
    private TabHost mTabHost;

    // @Override
    // protected void onCreate(Bundle savedInstanceState) {
    // super.onCreate(savedInstanceState);
    // setContentView(R.layout.activity_main);
    //
    // mViewPager = (ViewPager) findViewById(R.id.viewpager);
    //
    // // Tab Initialization
    // initialiseTabHost();
    //
    // // Fragments and ViewPager Initialization
    // List<Fragment> fragments = getFragments();
    // pageAdapter = new MyPageAdapter(getSupportFragmentManager(), fragments);
    // mViewPager.setAdapter(pageAdapter);
    // mViewPager.setOnPageChangeListener(MainActivity.this);
    // }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
	    Bundle savedInstanceState) {
	View v = inflater.inflate(R.layout.fragment_tabs_pager, container,
		false);
	mTabHost = (TabHost) v.findViewById(android.R.id.tabhost);
	mTabHost.setup();

	// TODO Put here your Tabs
	AddTab(this.mTabHost,
		this.mTabHost.newTabSpec("Tab1").setIndicator("Tab1"));
	AddTab(this.mTabHost,
		this.mTabHost.newTabSpec("Tab2").setIndicator("Tab2"));

	mTabHost.setOnTabChangedListener(this);
	return v;
    }

    // Method to add a TabHost
    private void AddTab(TabHost tabHost, TabHost.TabSpec tabSpec) {
	tabSpec.setContent(new MyTabFactory(getActivity()));
	tabHost.addTab(tabSpec);
    }

    // Manages the Tab changes, synchronizing it with Pages
    public void onTabChanged(String tag) {
	int pos = this.mTabHost.getCurrentTab();
	this.mViewPager.setCurrentItem(pos);
    }

    @Override
    public void onPageScrollStateChanged(int arg0) {
    }

    // Manages the Page changes, synchronizing it with Tabs
    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {
	int pos = this.mViewPager.getCurrentItem();
	this.mTabHost.setCurrentTab(pos);
    }

    @Override
    public void onPageSelected(int arg0) {
    }

    private List<Fragment> getFragments() {
	List<Fragment> fList = new ArrayList<Fragment>();

	// TODO Put here your Fragments
	SearchByLocation f1 = new SearchByLocation();
	SearchByLocation f2 = new SearchByLocation();
	fList.add(f1);
	fList.add(f2);

	return fList;
    }

    // Tabs Creation
    private void initialiseTabHost() {

    }

    class MyTabFactory implements TabContentFactory {

	private final Context mContext;

	public MyTabFactory(Context context) {
	    mContext = context;
	}

	public View createTabContent(String tag) {
	    View v = new View(mContext);
	    v.setMinimumWidth(0);
	    v.setMinimumHeight(0);
	    return v;
	}
    }

    class MyPageAdapter extends FragmentPagerAdapter {
	private List<Fragment> fragments;

	public MyPageAdapter(FragmentManager fm, List<Fragment> fragments) {
	    super(fm);
	    this.fragments = fragments;
	}

	@Override
	public Fragment getItem(int position) {
	    return this.fragments.get(position);
	}

	@Override
	public int getCount() {
	    return this.fragments.size();
	}
    }
}